package duyd.hibernate.training.test;

import java.util.Date;

import javax.persistence.EntityManager;

import org.junit.Test;

import duyd.hibernate.training.domain.Event;

public class HibernateLocking extends HibernateJPA {

    @Test
    public void test() {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        entityManager.persist(new Event("First Event", new Date()));
        entityManager.persist(new Event("Second Event", new Date()));
        entityManager.getTransaction().commit();
        entityManager.close();

        EntityManager entityManager1 = entityManagerFactory.createEntityManager();
        entityManager1.getTransaction().begin();
        Event original = entityManager1.find(Event.class, 1L);

        EntityManager entityManager2 = entityManagerFactory.createEntityManager();
        entityManager2.getTransaction().begin();
        Event modified = entityManager2.find(Event.class, 1L);

        original.setTitle(original.getTitle() + " (Original Edited)");
        entityManager1.getTransaction().commit();
        entityManager1.close();

        modified.setTitle(modified.getTitle() + " (Modified Edited)");
        entityManager2.getTransaction().commit();
        entityManager2.close();
    }

}
